package com.yeshine.opendata.controller.front.my.org;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.entity.Role;
import com.yeshine.opendata.service.IRoleService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by taohj on 2014/9/3.
 */
@Controller
@RequestMapping("/front/my/org/role")
public class MyRoleController extends AbstractFrontControllerBase {

    @Autowired
    private IRoleService service;

    @RequestMapping("createV")
    public String createV(
            @RequestParam("org") Integer org
    ) {
        setRequestAttr("org", org);
        return "/front/my/org/role/create";
    }




    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam("org") Integer org,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "description", required = false) String description
    ) {
        Role role = new Role();
        role.setName(name);
        role.setDescription(description);
        role.setOrg(new Org(org));
        Result<Role> result = service.create(role);
        //return  getReturnString(result,"redirect:/front/my/org/role/list?org="+org,setMessage("操作失败",result.getMessage()));
        return result.toString();
    }



    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam("id") Integer id
    ) {
        Role role = service.getById(id).getTarget();
        setRequestAttr("role",role);
        return "/front/my/org/role/modify";
    }




    @ResponseBody
    @RequestMapping("modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "description", required = false) String description
    ) {
        Role role = service.getById(id).getTarget();
        role.setName(name);
        role.setDescription(description);
        Result<Role> result = service.modify(role);
        return result.toString();
    }

    @ResponseBody
    @RequestMapping("remove")
    public  String remove(
            @RequestParam("id") Integer id
    ){
        Result<Role> result=service.remove(new Role(id));
        return  result.toString();
    }


    @RequestMapping("list")
    public  String list(
            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "size",required = false) Integer size,
            @RequestParam("org") Integer org,
            @RequestParam(value = "name",required = false) String name
    ){

        Role role=new Role();
        role.setName(name);
        role.setOrg(new Org(org));
        PageResult<Role> pageResult=new PageResult<Role>(page,size);
        pageResult.setT(role);
        Result<Role> result=service.getByPage(pageResult);
        setRequestAttr("result",result);
        setRequestAttr("org",org);
        return  "front/my/org/role/list";
    }

}
